package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.Exercicio;
import models.GrupoMuscular;




public class DAOGrupoMuscular {

	public Set<GrupoMuscular> select() throws Exception {
		Set<GrupoMuscular> objs = new HashSet<GrupoMuscular>();
		try {

			String query = "select idGrupoMuscular, nome from GrupoMuscular";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				GrupoMuscular obj = new GrupoMuscular();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs; 
	}

	public GrupoMuscular select(int id) throws Exception {
		GrupoMuscular obj = null;
		try	{

			String query = "select idGrupoMuscular, nome from GrupoMuscular where idGrupoMuscular = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, id);
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new GrupoMuscular();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}
	
	public GrupoMuscular select(String nome) throws Exception {
		GrupoMuscular obj = null;
		try	{

			String query = "select idGrupoMuscular, nome from GrupoMuscular where nome = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setString(1, nome);
			ResultSet resultSet = statement.executeQuery();

			if (resultSet.next()) {
				obj = new GrupoMuscular();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				
				System.out.println(obj.id);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public void insert(GrupoMuscular f) throws Exception {
		try {
			String insert = "insert into GrupoMuscular(nome) values(?)";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

			statement.setString(1, f.nome);

			statement.executeUpdate();
		} catch(SQLException e) {	
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void update(GrupoMuscular f) throws Exception {
		try {

			String update = "update GrupoMuscular set nome = ? where idGrupoMuscular = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

			statement.setInt(2, f.id);
			statement.setString(1, f.nome);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(GrupoMuscular f) throws Exception {
		try {

			String delete = "delete from GrupoMuscular where idGrupoMuscular = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

			statement.setInt(1, f.id);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}
